using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using MapWindow.Geometries;
namespace MapWindow.Drawing
{
    /// <summary>
    /// This is an OGC class, but isn't as versitile as some other options
    /// </summary>
    public class LineSymbolizer : FeatureSymbolizer, ILineSymbolizer
    {
        /// <summary>
        /// The Geometry to actually draw.  
        /// </summary>
        protected IGeometry _Geometry;

        /// <summary>
        /// Not implemented
        /// </summary>
        protected Stroke _Stroke;

        /// <summary>
        /// Not implemented
        /// </summary>
        protected float _PerpendicularOffset;

        /// <summary>
        /// A list of pens to use when drawing
        /// </summary>
        protected List<Pen> _FillPens;

        ILineSymbolizer _DefaultLineProvider;
        Image _LegendImage;

        /// <summary>
        /// Constructor for LineSymbolizer
        /// </summary>
        public LineSymbolizer() : base()
        {
            _BorderVisible = TrueFalse.False;
            _FillPens = new List<Pen>();

            // Pens should be darker than the usual fill colors
            _FillColor = RandomHue(.8, .3);
            _FillPens.Add(new Pen(_FillColor));
           
         
        }
        

        /// <summary>
        /// This creates a new instance of the LineSymbolizer, but
        /// uses the specified ILineSymbolizer to obtain any default
        /// values.
        /// </summary>
        /// <param name="defaultProvider"></param>
        public LineSymbolizer(ILineSymbolizer defaultProvider) : base(defaultProvider)
        {
            _DefaultLineProvider = defaultProvider;
        }

        /// <summary>
        /// Copies all the point symbol properties from any valid implementation of ILineSymbolizer
        /// like a MapLineString 
        /// </summary>
        /// <param name="copyFrom"></param>
        public void Copy(ILineSymbolizer copyFrom)
        {

            _BorderColor = copyFrom.BorderColor;
            _BorderPen = copyFrom.BorderPen;
            _BorderWidth = copyFrom.BorderWidth;
            if (copyFrom.BorderVisible == true)
            {
                _BorderVisible = TrueFalse.True;
            }
            else
            {
                _BorderVisible = TrueFalse.False;
            }


            _FillBrush = copyFrom.FillBrush;
            _FillColor = copyFrom.FillColor;

        }

        /// <summary>
        /// Since lines can be infinite, this will draw a straight line from the left to the right
        /// of the specified target.  clipRectangle is ignored.
        /// </summary>
        /// <param name="g"></param>
        /// <param name="clipRectangle"></param>
        /// <param name="target"></param>
        public override void Draw(Graphics g, Rectangle clipRectangle, Rectangle target)
        {
            if (target.IsEmpty) return;
            int center = target.Top + target.Height /2 ;
            if (BorderVisible == true)
            {
                g.DrawLine(BorderPen, target.Left, center, target.Right, center);
            }
            foreach(Pen fp in FillPens)
            {
                g.DrawLine(fp, target.Left, center, target.Right, center);
            }

        }

        #region Properties

        /// <summary>
        /// A list of pens.  This is the real set of items that control drawing.
        /// Setting them directly enables just about any GDI+ supported linear
        /// drawing.
        /// </summary>
        public List<Pen> FillPens
        {
            get { return _FillPens; }
            set { _FillPens = value; }
        }

        /// <summary>
        /// Setting this will also set the color for all the fill pens,
        /// so it should be used with caution.
        /// </summary>
        public override Color FillColor
        {
            set
            {
                _FillColor = value;
                _FillBrush = new SolidBrush(value);
                if (_FillPens == null)
                {
                    _FillPens = new List<Pen>();
                    _FillPens.Add(new Pen(_FillBrush));
                }
                else
                {
                    foreach (Pen p in FillPens)
                    {
                        p.Color = _FillColor;
                    }
                }
            }
        }

        /// <summary>
        /// Specifies a geometry to be used for the line drawing.  If none is specified, then all the geometries
        /// of the containing feature will be used.
        /// </summary>
        public IGeometry Geometry
        {
            get { return _Geometry; }
            set { _Geometry = value; }
        }

        /// <summary>
        /// NOT IMPLEMENTED - Not sure how to do this in dot net, but theoretically
        /// is a part of OGC syntax
        /// </summary>
        public float PerpendicularOffset
        {
            get { return _PerpendicularOffset; }
            set { _PerpendicularOffset = value; }
        }

        /// <summary>
        /// Specifies a drawing pattern using OGC styles 
        /// </summary>
        public Stroke Stroke
        {
            get { return _Stroke; }
            set { _Stroke = value; }
        }

        

        /// <summary>
        /// Gets or sets an image to use for the legend.  This is not currently
        /// automatically calculated.
        /// </summary>
        public Image LegendImage
        {
            get { return _LegendImage; }
            set { _LegendImage = value; }
        }

        #endregion


    }
}
